package o2o.web.actions.sys;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.system.SysSettingMobileMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.sys.SysSettingMobile;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.sys.SysSettingMobileVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//系统设置

@Controller
public class SysSettingsUserAction {

    /**
     * 手机端
     */
    //用户获取系统配置
    @RequestMapping(method = RequestMethod.POST, value = "/user/sys/mobile/settings/list")
    @ResponseBody
    public String sys_user_list(String token) {
        List<SysSettingMobile> list = SysSettingMobileMemory.getInstance().listOfAllShow();
        if (ValidateUtil.isAbsList(list)) {
            List<SysSettingMobileVO> voList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }

                SysSettingMobileVO vo = new SysSettingMobileVO();
                try {
                    ObjectUtils.copyPropertiesExclude(list.get(i), vo);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                voList.add(vo);
            }

            if (ValidateUtil.isAbsList(voList)) {
                return AjaxResult.successResult(voList.size(), voList.size(), voList);
            }
        }

        return AjaxResult.successResult(0, 0, null);
    }


    /**
     * 管理员
     */
    //获取所有系统设置
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/mobile/settings/list")
    @ResponseBody
    public String sys_list(String token) {

        List<SysSettingMobile> list = SysSettingMobileMemory.getInstance().listOfAll();
        if (ValidateUtil.isAbsList(list)) {
            List<SysSettingMobileVO> voList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }

                SysSettingMobileVO sysSettingVO = new SysSettingMobileVO();
                try {
                    ObjectUtils.copyPropertiesExclude(list.get(i), sysSettingVO);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                voList.add(sysSettingVO);
            }
            if (ValidateUtil.isAbsList(voList)) {
                return AjaxResult.successResult(voList.size(), voList.size(), voList);
            }
        }

        return AjaxResult.successResult(0, 0, null);
    }

    //修改系统设置
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/mobile/settings/modify")
    @ResponseBody
    public String sys_modify(String token, SysSettingMobileVO info) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }

        //验证是否有登录权限
//        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
//            return AjaxResult.errorResult("您没有访问权限");
//        }

        if (null == info) {
            return AjaxResult.errorResult("请补全信息");
        }

        if (ValidateUtil.isNull(info.getCode())) {
            return AjaxResult.errorResult("code不能为空");
        }


        SysSettingMobile sysSetting = SysSettingMobileMemory.getInstance().getByCode(info.getCode());
        if (null == sysSetting) {
            return AjaxResult.errorResult("未找到该项设置，请查证");
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.SYS_SETTING_MOBILE).append(" set val='").append(info.getVal()).append("',descs='").append(info.getDescs())
                .append("',name='").append(info.getName()).append("',note='").append(info.getNote()).append("',updateUser='")
                .append(null == user ? "" : user.getUsername()).append("' where delFlag = 0 and code = '").append(info.getCode()).append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }

        //修改缓存
        SysSettingMobileMemory.getInstance().modifyByCode(info.getCode());//修改信息

        return AjaxResult.successResult();
    }


    //新增系统参数
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/mobile/settings/add")
    @ResponseBody
    public String sys_add(String token, SysSettingMobile info) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }

//        //验证是否有登录权限
//        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
//            return AjaxResult.errorResult("您没有访问权限");
//        }

        if (null == info) {
            return AjaxResult.errorResult("请补全信息");
        }

        if (ValidateUtil.isNull(info.getCode())) {
            return AjaxResult.errorResult("code不能为空");
        }


        SysSettingMobile sysSetting = SysSettingMobileMemory.getInstance().getByCode(info.getCode());
        if (null != sysSetting) {
            return AjaxResult.errorResult("code 已存在，请更换");
        }

        if (null != user) {
            info.setCreateUser(null == user ? "" : user.getUsername());
        }
        info.setCreateTime(DateUtils.getCurrTime());

        try {
            UserDaoSql.getInstance().insertObj(PojoTables.SYS_SETTING_MOBILE, info);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("新增失败，请稍后再试");
        }
        //修改缓存
        SysSettingMobileMemory.getInstance().modifyByCode(info.getCode());//修改信息

        return AjaxResult.successResult();
    }

}
